package com.uidp.domain;

import static com.uidp.bundle.Messages.GROUP_NAME_NOT_FOUND;

import java.util.List;

import com.acsior.dac.exeption.DataAccessException;
import com.acsior.sbc.exception.BusinessException;
import com.acsior.sbc.exception.SearchException;
import com.acsior.sbc.exception.ValidationException;
import com.acsior.sbc.impl.hibernate.HibernateBusinessObject;
import com.uidp.hibernatelayer.AttributesDAO;
import com.uidp.hibernatelayer.AttributesVO;
import com.uidp.search.AttributeSearch;

public class Attributes extends
		HibernateBusinessObject<AttributesVO, AttributesDAO> {

	/**
	 * 
	 * @param username
	 *            - потребителско име
	 * @return IUser - резултат
	 * 
	 * @throws BusinessException
	 * @throws SearchException
	 */
	public static List<Attributes> searchAttributes(ModulElements modulElements)
			throws BusinessException, SearchException {
		AttributeSearch search = new AttributeSearch();
		search.setModulEl(modulElements);
		search.execute();

		if (search.getResult().isEmpty()) {
			throw new BusinessException(GROUP_NAME_NOT_FOUND,
					modulElements.getElementname());
		}

		return search.getResult();
	}

	private AttributeType attributeType;
	private ModulElements modulElements;
	private String uniqname;
	private String attributeName;
	private Boolean isRequired;
	private Integer ordering;

	public Attributes() throws BusinessException {
		super();
	}

	public Attributes(AttributesVO h) throws BusinessException {
		super(h);
	}

	/** @see PersistentObject#initialize() */
	@Override
	protected void initialize() throws BusinessException {
		super.initialize();
		setModulElements(new ModulElements());
		setAttributeType(new AttributeType());
	}

	@Override
	protected void setValues(AttributesVO h) throws DataAccessException {
		h.setIsRequired(getIsRequired());
		h.setUniqname(getUniqname());
		h.setAttributeName(getAttributeName());
		h.setAttributeType(getAttributeType().findData());
		h.setModulElements(getModulElements().findData());
		h.setOrdering(getOrdering());

	}

	@Override
	protected void copyProperties(AttributesVO d, boolean deepCopy)
			throws BusinessException {
		setUniqname(d.getUniqname());
		setIsRequired(d.getIsRequired());
		setAttributeType(new AttributeType(d.getAttributeType()));
		setModulElements(new ModulElements(d.getModulElements()));
		setAttributeName(d.getAttributeName());
		setOrdering(d.getOrdering());

	}

	@Override
	protected void validate() throws ValidationException {
		// TODO Auto-generated method stub

	}

	@Override
	protected Class<AttributesDAO> getDAOClass() {
		return AttributesDAO.class;
	}

	public AttributeType getAttributeType() {
		return attributeType;
	}

	public void setAttributeType(AttributeType attributeType) {
		this.attributeType = attributeType;
	}

	public ModulElements getModulElements() {
		return modulElements;
	}

	public void setModulElements(ModulElements modulElements) {
		this.modulElements = modulElements;
	}

	public String getUniqname() {
		return uniqname;
	}

	public void setUniqname(String uniqname) {
		this.uniqname = uniqname;
	}

	public Boolean getIsRequired() {
		return isRequired;
	}

	public void setIsRequired(Boolean isRequired) {
		this.isRequired = isRequired;
	}

	public String getAttributeName() {
		return attributeName;
	}

	public void setAttributeName(String attributeName) {
		this.attributeName = attributeName;
	}

	public Integer getOrdering() {
		return ordering;
	}

	public void setOrdering(Integer ordering) {
		this.ordering = ordering;
	}


}
